package cn.schoolwow.quickdao.test.module.business.operation.flow;

import cn.schoolwow.quickdao.QuickDAO;
import cn.schoolwow.quickdao.domain.DAO;
import cn.schoolwow.quickflow.domain.FlowContext;
import cn.schoolwow.quickflow.flow.BusinessFlow;
import com.zaxxer.hikari.HikariDataSource;

public class SetSourceDAOFlow implements BusinessFlow {
    @Override
    public void executeBusinessFlow(FlowContext flowContext) throws Exception {
        setDataSource(flowContext);
        setDAO(flowContext);
    }

    @Override
    public String name() {
        return "设置数据源对象";
    }

    private void setDataSource(FlowContext flowContext){
        HikariDataSource hikariDataSource = new HikariDataSource();
        hikariDataSource.setDriverClassName("com.mysql.jdbc.Driver");
        hikariDataSource.setJdbcUrl("jdbc:mysql://localhost:3306/smart_work_flow?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=true&serverTimezone=GMT%2B8");
        hikariDataSource.setUsername("root");
        hikariDataSource.setPassword("123456");

        flowContext.putTemporaryData("hikariDataSource", hikariDataSource);
    }

    private void setDAO(FlowContext flowContext){
        HikariDataSource hikariDataSource = (HikariDataSource) flowContext.checkData("hikariDataSource");

        DAO dao = QuickDAO.newInstance()
                .dataSource(hikariDataSource)
                .build();
        flowContext.putTemporaryInstanceData(dao, DAO.class);
    }
}
